package 笔试2.美团.four;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


public class Main {
    static int[] p;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a=sc.nextInt(),b=sc.nextInt();
        p=new int[a];
        int tS=2;
        for (int i = 0; i < a; i++) {
            p[i]=sc.nextInt();
        }

        int res = back(p,tS-1);
        System.out.println(res);
    }

    static Map<String, Integer> map =new HashMap<>();
    static int back(int[] p, int tS) {
        String key= Arrays.toString(p)+","+tS;
        if(tS==0){
            return dajianxiao(p);
        }

        if(map.containsKey(key))return map.get(key);

        int min=99999;
        for (int i = 0; i < p.length-1; i++) {
            int[] a = shuangbiqieshuzu(0,i);
            int[] b = shuangbiqieshuzu(i+1,p.length-1);

            int s=0;
            if(a.length==1){
                s+=0;
            }else if(a.length>1){
                s+=back(a,tS-1);
            }

            if(b.length==1){
                s+=0;
            }else if(b.length>1){
                s+=back(b,tS-1);
            }
            min=Math.min(s,min);
        }

        map.put(key,min);
        return min;
    }

    static int[] shuangbiqieshuzu(int s, int e) {
        int[] t = new int[0];
        if(s>e){
            return t;
        }

        int[] newp=new int[e-s+1];
        int f=0;
        for (int i =s; i <=e; i++) {
            newp[f]=p[i];
            f++;
        }
        return newp;
    }

    static int dajianxiao(int[] p){
        if(p.length==1)return 0;
        Arrays.sort(p);
        return p[p.length-1]-p[0];
    }
}
